class CacheManager {
  constructor(expire = 20 * 60 * 1000, maxSize = 50) {
    this.cache = new Map();
    this.expire = expire;
    this.maxSize = maxSize;
  }

  set(key, value) {
    key = JSON.stringify(key);//将对象转化为Json字符

    if (this.cache.size >= this.maxSize) {
      this.cache.delete(this.cache.keys().next().value);  // 删除最旧的缓存
    }
    this.cache.set(key, { value, timestamp: Date.now() });
  }

  get(key) {
    key = JSON.stringify(key);//将对象转化为Json字符
    const data = this.cache.get(key);
    if (!data) return null;

    if (Date.now() - data.timestamp > this.expire) {
      this.cache.delete(key);
      return null;
    }

    return data.value;
  }

  clear() {
    this.cache.clear();
  }
}
const Cache = new CacheManager()
export default Cache;  